<?php
/*
   mysql 数据表 安装类
 */
include("../inc/dbFun.php");
   class InstallMysqlTable{
   	
   	   private $fileName = "./#tableFrame.zip";
   	   private $host;
   	   private $user;
   	   private $pass;
   	   private $dbName;
   	   private $pageNum;
   	   private $founder;
   	   private $webName;
   	   
   	   private $smtpHost;
   	   private $smtpUser;
   	   private $smtpPass;
   	   private $smtpFromName;
 	      	   
   	   public  $state;
   	   
   	   //初始化数据库连接
   	   public function InstallMysqlTable($host, $user, $pass, $database,$pageNum, $founder, $webname ,$smtpHost,$smtpUser,$smtpPass,$smtpFromName ){
   	   	   $con = DB_Connection($host , $user ,$pass ,$database);    	   	
	   	    if (empty($con)) {
	   	    	$this->state = 0;
	   	    }else {
				$this->state = 1;
	   	        $this->host = $host;
	   	        $this->user = $user;
	   	        $this->pass = $pass;
	   	        $this->pageNum = $pageNum;
	   	        $this->founder = $founder;
	   	        $this->webName = $webname;
	   	        $this->dbName = $database;
	   	        
	   	        $this->smtpHost = $smtpHost;
	   	        $this->smtpUser = $smtpUser;
	   	        $this->smtpPass = $smtpPass;
	   	        $this->smtpFromName = $smtpFromName;
 
	   	    }
   	   }
   	   
   	   
   	   //创建表
   	   public function insertTable()
   	   {
		  $dir = './install/';
   	   	  if (file_exists($this->fileName)) {
			    $zip = new ZipArchive();
				$rs = $zip->open($this->fileName);
				$zip->extractTo($dir);
				$zip->close();
   	   	  	 if (is_dir($dir)) {
				 $handle = opendir($dir);
				 while (false !== ($file = readdir($handle))) {
				   if ($file != "." && $file!="..") {
					    $f = fopen($dir.$file , 'r');
						$text =  fread($f , filesize($dir.$file));
						fclose($f);
						$tableName = explode("." , $file);
						DB_Query('drop table if exists '.$tableName[0]);
						DB_Query($text);
						unlink($dir.$file);
					}
				 } 
				closedir( $handle );
				rmdir($dir);               
   	   	  	 }
   	   	  }
   	   }
   	   
   	   //插入基本数据
   	   public function insertBasicData($name,$pass){
   	   	 DB_Query('insert into userInfo (id , name , pass , type , departmentId ) values (1 , "'.$name.'" , "'.sha1($pass).'" , 2,1)') or dir("插入失败");
   	   	 DB_Query('insert into department values(1 ,"市场部")') or die("错误啦！");
   	   	 DB_Query('insert into resource values(1 ,"鸿雁网络行为管理系统")') or die("错误啦！");
   	   }
   	   
   	   //写入配置文件
   	   public function insertWriteConfig(){
   	      $br = chr(10).chr(13);
   	   	  $str = '<?php'.$br;
   	      $str .= 'define ( "WEB_HOME", dirname( dirname( __FILE__ ) ) ); #根目录服务器路径'.$br;
   	      $str .= 'define ( "WEB_NAME", "'.$this->webName.'" ); 		  #网站名字'.$br;
   	      $str .= 'define ( "DB_HOSTNAME", "'.$this->host.'" ); 		  #数据库服务器名字'.$br;
   	      $str .= 'define ( "DB_USERNAME", "'.$this->user.'" ); 		  #数据库帐号'.$br;
   	      $str .= 'define ( "DB_PASSWORD", "'.$this->pass.'" ); 		  #数据库密码'.$br;
   	      $str .= 'define ( "DB_DATABASE", "'.$this->dbName.'" ); 		  #数据库名字'.$br;
   	      $str .= 'define ( "TPL_NAME", "default" );				   	  #默认模板名称'.$br; 
   	      $str .= 'define ( "FOUNDER" , "'.$this->founder.'"); 			  #创始人帐号'.$br;
   	      $str .= 'define ( "PAGESIZE" , '.$this->pageNum.');			  #分页数量'.$br;
		  $str .= 'define ( "STMPHOST" , "'.$this->smtpHost.'");		  #smtp邮件服务器'.$br;
		  $str .= 'define ( "STMP_USERNAME" , "'.$this->smtpUser.'"); 	  #smtp服务器登录帐号'.$br;
		  $str .= 'define ( "STMP_PASS" , "'.$this->smtpPass.'"); 		  #smtp服务器登录密码'.$br;
		  $str .= 'define ( "STMP_FROMNAME" , "'.$this->smtpFromName.'")  #发件人名称'.$br;
   	      $str .= '?>';
   	      $ftp = fopen('../inc/config.php' , 'w');
   	      fwrite($ftp , $str);
   	      fclose($ftp);   	   	 
   	   }
   	
   	
   }
  

?>